package com.cpi.cornpp.futurepurchase.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.futurepurchase.form.CustomerFuturePurchaseForm;
import com.cpi.cornpp.futurepurchase.services.FuturePurchaseService;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseByCustomerView;
import com.cpi.cornpp.period.exception.PeriodException;
import com.cpi.cornpp.period.service.PeriodService;
import com.cs.common.utils.NumberUtil;

/**
 * 
 * @author Edgar Joao
 *
 */
public class FuturePurchaseByCustomerEditAction extends MappingDispatchAction {
	/** Identifier default workflow*/
	private final static String DEFAULT = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$		
	//private static final String ADD_PRODUCTS_TO_LIST = "error.message.futurepurchase.add.products.to.list";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	/** Identifier fail workflow*/
	//private static final String FAILURE = "fail";
	private static final String LIST_CUSTOMER_PURCHASE_PRODUCTS = "LIST_CUSTOMER_PURCHASE_PRODUCTS";
	private static final String EDIT_PARAMETER_ID = "cornFutureDistributionId";
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private final static String FUTURE_IN_SESSION_LABEL = "FuturePurchaseByCustomerView";//$NON-NLS-1$
	
	/**
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goEditFuturePurchaseByCustomer(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		PeriodService periodService = null;					
		HttpSession httpSession = null;
		CustomerFuturePurchaseForm customerFuturePurchaseForm = null;
		List periodAccount = null;
		List listInSession = null;	
		FuturePurchaseService futurePurchaseService = null;
		String cornFutureDistributionId = null;
		FuturePurchaseByCustomerView futurePurchaseByCustomerView = null;
													
		try{
			
			httpSession = request.getSession(false);		
			periodAccount = new ArrayList(0);				
			periodService = new PeriodService();			
			futurePurchaseService = new FuturePurchaseService();
			customerFuturePurchaseForm = (CustomerFuturePurchaseForm) form;
			
			cornFutureDistributionId = request.getParameter(EDIT_PARAMETER_ID);
			if(cornFutureDistributionId == null){
				cornFutureDistributionId = (String) httpSession.getAttribute(EDIT_PARAMETER_ID);
			}else{
				httpSession.setAttribute(FUTURE_IN_SESSION_LABEL, null);
			}
			httpSession.setAttribute(EDIT_PARAMETER_ID, cornFutureDistributionId);
			
			futurePurchaseByCustomerView = (FuturePurchaseByCustomerView) httpSession.getAttribute(FUTURE_IN_SESSION_LABEL);
			if( futurePurchaseByCustomerView == null ){
				futurePurchaseByCustomerView = futurePurchaseService.getFuturePurchaseCustomerViewById(NumberUtil.getInt(cornFutureDistributionId));
			}
			customerFuturePurchaseForm.setFuturePurchaseByCustomerView(futurePurchaseByCustomerView);
			
			cornFutureDistributionId = request.getParameter(EDIT_PARAMETER_ID);
			if(cornFutureDistributionId != null){
				listInSession = customerFuturePurchaseForm.getFuturePurchaseByCustomerView().getListProducts();
				httpSession.setAttribute(LIST_CUSTOMER_PURCHASE_PRODUCTS, listInSession);
			}else{
				listInSession = (ArrayList)httpSession.getAttribute(LIST_CUSTOMER_PURCHASE_PRODUCTS);
				customerFuturePurchaseForm.getFuturePurchaseByCustomerView().setListProducts(listInSession);	
			}
			
			periodAccount = periodService.getActivePeriods();			
			httpSession.setAttribute("periodAccount", periodAccount);
			
		}catch (PeriodException costElementException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, costElementException);
			throw costElementException;
		}	
		
		return mapping.findForward(DEFAULT);
	}
	/**
	 * Editar detalle del cliente
	 */
	public ActionForward editClientDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		FuturePurchaseService futurePurchaseService = null;
		List listInSession = null;		
		String forward = null;
		MessageResources messageResources = null;
		String messageError = null;		
		CustomerFuturePurchaseForm customerFuturePurchaseForm = null;
		customerFuturePurchaseForm = (CustomerFuturePurchaseForm) form;
		HttpSession httpSession = null;		
		httpSession = request.getSession(false);
		futurePurchaseService = new FuturePurchaseService();
		forward = DEFAULT;
		
		//Verificar si se ha seleccionado almenos un producto
		listInSession = (List) httpSession.getAttribute(LIST_CUSTOMER_PURCHASE_PRODUCTS);
		messageResources = getResources(request);			
		if(listInSession != null){
			customerFuturePurchaseForm.getFuturePurchaseByCustomerView().setListProducts(listInSession);
		}else{
			customerFuturePurchaseForm.getFuturePurchaseByCustomerView().setListProducts(new ArrayList());
		}
		futurePurchaseService.updateFuturePurchaseByCustomer(customerFuturePurchaseForm.getFuturePurchaseByCustomerView());
		messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
		httpSession.setAttribute(MESSAGE, messageError);
		
		form.reset(mapping, request);
		return mapping.findForward(forward);
	}
}
